<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let obj = {
            id: 4,
            left: {
                id: 2,
                left: {
                    id: 1,
                    left: null,
                    right: null
                },
                right: {
                    id: 3,
                    left: null,
                    right: null
                }
            },
            right: {
                id: 7,
                left: {
                    id: 6,
                    left: null,
                    right: null
                },
                right: {
                    id: 9,
                    left: null,
                    right: null,
                }
            },
        }
        /*
            左数转为右树，右树转为左树
        */
        function invertTree(tree) {
            /*
                只管一层，本层交换之后，递归调用invertTree操作左树和右树
            */
            if(tree !== null) {
                let temp = tree.left;
                tree.left = tree.right;
                tree.right = temp;
                invertTree(tree.left);
                invertTree(tree.right);
            }
            return tree;
        }
        console.log(invertTree(obj));
    </script>
</body>
</html>